ext.define('ext', function() {

var messages = extension.messages;
var utils = extension.utils;
var requests = extension.requestview;
var filters = extension.filterview;
var tab_url;
var tab_id;
var exports;

function toolbarClicked(e) {
    if (e.relayTarget.classList.contains('selected'))
        return;
    var el = e.currentTarget.querySelector('.selected');
    el.classList.remove('selected');
    el = el.children[0];
    $q(el.getAttribute('href')).classList.remove('selected');
    el = e.relayTarget;
    el.classList.add('selected');
    el = el.children[0];
    $q(el.getAttribute('href')).classList.add('selected');
}

function blockContextMenu(e) {
    if (e.target.classList.contains('glass-pane'))
        e.preventDefault();
}

function checkDomainChanged(id, info, tab) {
    if (id == tab_id) {
        if (info.status == 'loading')
            startInspector();
    }
}

function checkChannelSwitched(id, closed) {
    if (closed == tab_id) {
        window.location.hash = id;
        resetInspector();
    }
}

function startInspector() {
    chrome.tabs.get(tab_id, function(t) {
        var url = t.url.replace(/#.*/, '');
        var sameurl = tab_url == url;
        var samedomain = utils.authority(url) == utils.authority(tab_url);
        var permitted = utils.permitted(url);
        tab_url = url;
        document.title = utils.strings.TITLE + tab_url;
        if (!sameurl) {
            if (!samedomain)
                requests.reset();
        }
        filters.reset();
        if (permitted) {
            filters.update({
                url: url,
                filters: exports.getFilters()
            });
            inspectIsolatedWorld();
        }
    });
}

function resetInspector() {
    tab_id = +window.location.hash.substring(1);
    tab_url = 'chrome://null';
    startInspector();
}

function shutdownInspector(id) {
    if (id == tab_id)
        window.close();
}

function inspectIsolatedWorld() {
    sendMessage('inspect');
}

function filterIsolatedWorld(data) {
    sendMessage('filter', data);
}

function sendMessage(type, data) {
    chrome.tabs.sendMessage(tab_id, {type: type, info: data});
}

function receiveMessage(message, sender) {
    if (sender.tab.id == tab_id) {
        if (message.type === 'audit') {
            requests.update({
                url: sender.url,
                info: message.info.plugin
            });
        }
    }
}

return {
    initialize: function() {
        utils.process([
            function(pass) {
                chrome.runtime.getBackgroundPage(function(wnd) {
                    exports = wnd.extension.exports;
                    pass();
                });
            },
            function(pass) {
                chrome.runtime.getPlatformInfo(function(info) {
                    document.body.classList.add('platform-' + info.os);
                    pass();
                });
            }],
            function() {
                document.body.addEventListener('contextmenu', blockContextMenu);
                relayEvent('.navigation', 'mousedown', 'li', toolbarClicked);
                chrome.tabs.onRemoved.addListener(shutdownInspector);
                chrome.tabs.onUpdated.addListener(checkDomainChanged);
                chrome.tabs.onReplaced.addListener(checkChannelSwitched);
                chrome.runtime.onMessage.addListener(receiveMessage);
                requests.bind();
                messages.listen({
                    'requests-reload': resetInspector,
                    'filters-update': filterIsolatedWorld
                });
                resetInspector();
            }
        );
    }
};

});

document.addEventListener('DOMContentLoaded', ext.initialize);
